package com.jiazhong.basic.borrow.dao;

import com.jiazhong.basic.borrow.bean.Book;
import com.jiazhong.basic.borrow.db.DBManager;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BookDAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    @SneakyThrows
    public List<Book> find() {
        List<Book> list = new ArrayList<>();
        conn = DBManager.getDruidConnection();
        String sql = "select * from book";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            Book book = new Book();
            book.setId(rs.getInt(1));
            book.setName(rs.getString(2));
            book.setAuthor(rs.getString(3));
            book.setNumber(rs.getInt(4));
            book.setDesc(rs.getString(5));
            list.add(book);
        }
        return list;
    }
    @SneakyThrows
    public boolean shifouchongzu(int id){
        conn = DBManager.getDruidConnection();
        String sql = "select number from book where id=?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        rs = ps.executeQuery();
        if(rs.next()){
            return rs.getInt(1) > 0;
        }
        return false;
    }
}
